const User = require('./user');
const Class = require('./class');
const Course = require('./course');

// 在设置关系之前，确保所有模型都已经定义
setTimeout(() => {
  // 定义模型关系
  User.belongsTo(Class, { 
    foreignKey: 'classId',
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE'
  });
  
  Class.hasMany(User, { 
    foreignKey: 'classId',
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE'
  });

  Class.belongsTo(User, { 
    foreignKey: 'teacherId',
    as: 'teacher',
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE'
  });
  
  Course.belongsTo(User, { 
    foreignKey: 'teacherId',
    as: 'teacher',
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE'
  });
  
  Course.belongsTo(Class, { 
    foreignKey: 'classId',
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE'
  });
}, 0);

module.exports = {
  User,
  Class,
  Course
};